package service

import (
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"lib/structs/base"
	"lib/structs/enum"
	"lib/structs/vap"
	"vap/daemon/tcp"
	"vap/dao"
)

var SettingsService = new(settingsService)

type settingsService struct {
}

// CreateWinRdpAccount 创建 rdp 账户
func (s settingsService) CreateWinRdpAccount(accounts []string) {
	for _, item := range accounts {
		plug := vap.PluginInstruct{
			Cmd:             enum.BasePlugin,
			Args:            []string{"user", fmt.Sprintf("-ar=%s-qwer1234!", item)},
			TimeoutDuration: 10,
		}
		bs, err := json.Marshal(plug)
		if err != nil {
			logrus.Error(err)
			continue
		}
		inst := base.ClientInstruct{
			Type: enum.CreateRdpWinAccount,
			Body: string(bs),
		}
		li, _, err := dao.HostRepository.FindHostAll()
		if err != nil {
			logrus.Error(err)
			continue
		}
		for _, item := range li {
			host, err := dao.HostRepository.FindHostById(item.Id)
			if err != nil {
				logrus.Error(err)
				continue
			}
			err = tcp.TcpServer.SendInstruct(host.Uid, inst)
			if err != nil {
				logrus.Error(err)
			}
		}
	}
	return
}
